import {
  showText
} from '@/utils/util'
import {
  editgemomemberuser,getgemomemberuser
} from '@/api/my'
Component({
  properties: {
    show: { // 是否显示组件
      type: Boolean,
      value: false
    },
    avatarUrl: {
      type: String,
      value: ''
    },
    name:{
      type: String,
      value: ''
    }
  },
  lifetimes:{
    attached(){
      this.setData({
        nickName: this.data.name || ''
      })
    }
  },
  data: {
    imgUri: getApp().globalData.imgUri,
    nickName: ''
  },
  methods: {
    /**
     * 获取头像
     */
    bindchooseavatar(e) {
      // 临时路径 e.detail.avatarUrl
      this.setData({
        avatarUrl: e.detail.avatarUrl
      })
      this.triggerEvent('avatarUrl', e.detail.avatarUrl)
    },
    getInputVal(e) {
      console.log(e)
      this.setData({
        nickName: e.detail.value
      })
    },
    /**
     * 确定事件（调用父组件方法）
     */
    confirm() {
      if (!this.data.avatarUrl) {
        showText('请选择头像')
        return
      }

      if(this.data.avatarUrl.indexOf("avatarUrl.png") >= 0){
        showText('请选择头像')
        return
      }
      if (!this.data.nickName) {
        showText('请输入昵称')
        return
      }
      if(this.data.nickName == "GEMO会员"){
        showText('请输入昵称')
        return
      }
      console.log(this.data);
      let params = {
        avatarUrl: this.data.avatarUrl,
        nickName: this.data.nickName,
      };
      // 如果params.avatarUrl 包含 getApp().globalData.cdn 则截取
      if (params.avatarUrl.includes(getApp().globalData.cdn)) {
        params.avatarUrl = params.avatarUrl.replace(getApp().globalData.cdn, '')
      }
      //params.avatarUrl = "";
      editgemomemberuser(params).then(res => {
        if (res.code == 200) {
          getgemomemberuser({}).then(res=>{
            if(res.code == 200){
              wx.setStorageSync('getgemomemberuser', res.data);
            }
          });
          this.triggerEvent('close',true)
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
          });
        }
      });
    },
    /**
     * 取消事件（调用父组件方法）
     */
    close() {
      this.triggerEvent('close',false)
    },
  }
})